package com.example;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.experimental.Accessors;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName: leetcode
 * @Description:
 * @Author: csp
 * @Date:
 */
public class FlatmapTest {
    @Data
    @Accessors(chain = true)
    class User {
        private String username;
        private String age;
        private String claz;
    }
    @Data
    @Accessors(chain = true)
    class Claz{
        private String clazNum;
        private String clazName;
    }
    @Data
    @Accessors(chain = true)
    @AllArgsConstructor
    class UserClaz {
        private String username;
        private String age;
        private String clazName;
    }
    @Test
    public void test() {
        List<User> userList = new ArrayList<>();
        List<Claz> clazList = new ArrayList<>();
        User user1 = new User().setUsername("123").setAge("18").setClaz("1");
        User user2 = new User().setUsername("456").setAge("20").setClaz("2");
        Claz claz1 = new Claz().setClazNum("1").setClazName("1班");
        Claz claz2 = new Claz().setClazNum("2").setClazName("2班");
        userList.add(user1);
        userList.add(user2);
        clazList.add(claz1);
        clazList.add(claz2);
        List<UserClaz> collect = userList.stream().map(user -> {
            Claz claz3 = clazList.stream().filter(claz -> claz.getClazNum().equals(user.getClaz())).findFirst().orElseGet(() -> new Claz());
            return new UserClaz(user.getUsername(), user.getAge(), claz3.getClazName());
        }).collect(Collectors.toList());
        System.out.println(collect);
    }
}
